#include <gtk/gtk.h>

/* radio button is derived from toggled button, so we hook 'toggled' signal. */
/*   when you click another radion button, the old and the new chosen buttons */
/*   both emit 'toggled' signal, so check if they are active  */


static gboolean cb(GtkWidget *widget, gpointer data)
{
  g_print("%d toggled", GPOINTER_TO_INT(data));
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
    g_print(" active *_*");
  g_print("\n");
  
  return FALSE;
}


int main(int argc, char *argv[])
{
  GtkWidget *window, *radio_button, *vbox;
  gint i;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  g_signal_connect(window, "destroy",
		   G_CALLBACK(gtk_main_quit), NULL);

  vbox = gtk_vbox_new(TRUE, 5);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  
  radio_button = gtk_radio_button_new_with_label(NULL, "radio button 0");
  g_signal_connect(radio_button, "toggled",
		   G_CALLBACK(cb), GINT_TO_POINTER(0));
  /* not good to use GINT_TO_POINTER and GPOINTER_TO_INT */

  gtk_box_pack_start(GTK_BOX(vbox), radio_button, TRUE, TRUE, 0);
  for (i = 1; i < 5; ++i)
    {
      gchar label_buffer[16];
      g_snprintf(label_buffer, sizeof(label_buffer), "radio button %d", i);
      radio_button = gtk_radio_button_new_with_label_from_widget
	(GTK_RADIO_BUTTON(radio_button), label_buffer);
      g_signal_connect(radio_button, "toggled",
		       G_CALLBACK(cb), GINT_TO_POINTER(i));
      gtk_box_pack_start(GTK_BOX(vbox), radio_button, TRUE, TRUE, 0);
    }

  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}
